#!/usr/bin/env python
# -*- coding: utf-8 -*-

from lexer import Lexer, get_lexer_by_name
from formatter import Formatter, get_formatter_by_name
from util import safestr

## ---- Render shortcut

def render_simple(code, lexer='FxWiki', formatter='HTML', outfile=None):
    if isinstance(formatter, str):
        formatter = get_formatter_by_name(formatter)
    if isinstance(lexer, str):
        lexer = get_lexer_by_name(lexer)

    formatter_obj = formatter()
    lexer_obj = lexer(formatter=formatter_obj)

    rst = lexer_obj.convert(code)

    if outfile:
        fobj = file(outfile, 'w+')
        fobj.write( safestr(rst) )
        fobj.close()
    else:
        return rst



## ---- mainline

if __name__ == '__main__':
    text = r'''

= h1 =

== h2 ==

=== h3 ===

==== h4 ====

===== h5 =====

====== h6 ======

>>> blockquote
Here is some
more line.

* List cmd
* List 2
* List 3

# List 12
# List 234


_italic_ *bold* `code`

|| header   || as   ||
|| 123      || asd  ||
|| 123      || asd  ||

[http://www.g.cn]

[http://www.g.cn|Google]

[./hi.png]

.. {{{ code :: lang:python
    def hi():
        print 123
.. }}}


    '''

    tmpl = r'''
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="generator" CONTENT="http://txt2tags.sf.net">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="stylesheet" TYPE="text/css" HREF="modern.css">
<TITLE>TXT2TAGS SAMPLE</TITLE>
</HEAD>
<BODY>

<div class="body">
%s
</div>

<!-- html code generated by txt2tags 2.5 (http://txt2tags.sf.net) -->
<!-- cmdline: txt2tags -\-style modern.css -o modern.html -\-css-sugar -\-toc -t html - -->
</BODY>
</HTML>
'''
    print tmpl % render_simple(text)

